Here we describe tree-based methods for regression and classification
These involve stratifying or segmenting the predictor space into a number of simple regions
Since the set of splitting rules used to segment the predictor space can be summarized in a tree, these types of approaches are known as decision-tree methods
Tree-based methods are simple and useful for interpretation
However they typically are not competitive with the best supervised learning approaches in terms of prediction accuracy
Hence we also discuss bagging, random forests, and boosting. These methods grow multiple trees which are then combined to yield a single consensus prediction
Combining a large number of trees can often result in dramatic improvements in prediction accuracy, at the expense of some loss interpretation
Decision trees can be applied to both regression and classification problems
We first consider regression problems, and then move on to classification
Example
Baseball Salary Data: How would you stratify it?
For the Hitters data, a regression tree for predicting the log salary of a baseball player, based on the number of years that he has played in the major leagues and the number of hits that he made in the previous year
At a given internal node, the label (of the form \(X_j < t_k\)) indicates the left-hand branch emanating from that split, and the right-hand branch corresponds to \(X_j > t_k\). For instance, the split at the top of the tree results in two large branches. The left-hand branch corresponds to Years$<\(4.5, and the right-hand branch corresponds to Years\)$4.5
The tree has two internal nodes and three terminal nodes, or leaves. The number in each leaf is the mean of the response for the observations that fall there
Results
Overall, the tree stratifies or segments the players into three regions of predictor space:
In keeping with the tree analogy, the regions \(R_1, R_2, R_3\) are known as terminal nodes
Decision trees are typically drawn upside down, in the sense that the leaves are at the bottom of the tree
The points along the tree where the predictor space is split are referred to as internal nodes
In the hitters tree, the two internal nodes are indicated by the text \(Years < 4.5\) and \(Hits < 117.5\)
Years is the most important factor in determining Salary, and players with less experience earn lower salaries than more experienced players
Given that a player is less experienced, the number of Hits that he made in the previous year seems to play little role in his Salary
But among players who have been in the major leagues for five or more years, the number of Hits made in the previous year does affect Salary, and players who made more Hits last year tend to have higher salaries
Surely an over-simplification, but compared to a regression model, it is easy to display, interpret and explain
We divide the predictor space – that is the set of possible values for \(X_1, X_2, ... , X_p\) – into \(J\) distinct and non-overlapping regions, \(R_1, R_2, ... , R_j\)
For every observation that falls into the region \(R_1\), we make the same prediction, which is simply the mean of the response values for the training observations in \(R_1\)
In theory, the regions could have any shape. However, we choose to divide the predictor space into high-dimensional rectangles, or boxes, for simplicity and for ease of interpretation of the resulting predictive model
The goal is to find boxes \(R_1, R_2, ... , R_j\) that minimize the RSS given by:
\(\sum_{j=1}^{j}\sum_{i \in R_j}(y_i - \hat{y}_{R_j})^2\)
Unfortunately, it is computationally infeasible to consider every possible partition of the feature space into \(J\) boxes
For this reason, we take a top-down, greedy approach that is known as recursive binary splitting
The approach is top-down because it begins at the top of the tree and then successively splits the predictor space; each split is indicated via two new branches further down on the tree
It is greedy because at each step of the tree-building process, the best split is made at that particular step, rather than looking ahead and picking a split that will lead to a better tree in some future step
We first select the predictor \(X_j\) and the cutpoint \(s\) such that splitting the predictor space into the regions \({X|X_j < s}\) and \({X|X_j \geq s}\)
Next, we repeat the process, looking for the best predictor and best cutpoint in order to split the data further so as to minimize the RSS within each of the resulting regions
However, this time, instead of splitting the entire predictor space, we split one of the two previously identified regions. We now have three regions
Again, we look to split one of these three regions further, so as to minimize the RSS. The process continues until a stopping criterion is reached; for instance, we may continue until no region contains more than five observations
We predict the response for a given test observation using the mean of the training observations in the region to which that test observation belongs
A five-region example of this approach is shown in the next slide
Top Left: A partition of two-dimensional feature space that could not result from recursive binary splitting
Top Right: The output of recursive binary splitting on a two-dimensional example
Bottom Left: A tree corresponding to the partition in the top right panel
Bottom Right: A perspective plot of the prediction surface corresponding to that tree
The process described above may produce good predictions on the training set, but is likely to overfit the data, leading to poor test set performance
A smaller tree with fewer splits (that is, fewer regions \(R_1, R_2, ... , R_j\)) might lead to lower variance and better interpretation at the cost of a little bias
One possible alternative to the process described above is to grow the tree only so long as the decrease in the RSS due to each split exceeds some (high) threshold
This strategy will result in smaller trees, but is too short-sighted: a seemingly worthless split early on in the tree might be followed by a very good split — that is, a split that leads to a large reduction in RSS later on
A better strategy is to grow a very large tree \(T_0\), and then prune it back in order to obtain a subtree
Cost complexity pruning — also known as weakest link pruning — is used to do this
we consider a sequence of trees indexed by a non-negative tuning parameter \(\alpha\). For each value of \(\alpha\) there corresponds a subtree \(T \subset T_0\) such that:
\(\sum_{m=1}^{|T|}\sum_{x_i \in R_m} (y_i - \hat{y}_{R_m})^2 + \alpha|T|\)
The tuning parameter α controls a trade-off between the subtree’s complexity and its fit to the training data
We select an optimal value \(\hat{\alpha}\) using cross-validation
We then return to the full data set and obtain the subtree corresponding to \(\hat{\alpha}\)
Use recursive binary splitting to grow a large tree on the training data, stopping only when each terminal node has fewer than some minimum number of observations
Apply cost complexity pruning to the large tree in order to obtain a sequence of best subtrees, as a function of \(\alpha\)
Use K-fold cross-validation to choose \(\alpha\). For each \(k = 1, ..., K\):
Repeat steps 1 and 2 on the \(\frac{K-1}{K}\)th fraction of the training data excluding the \(k_th\) fold
Evaluate the mean squared prediction error on the data in the left-out \(k_th\) fold, as a function of \(\alpha\)
Average the results, and pick \(\alpha\) to minimize the average error
Return the subtree from Step 2 that corresponds to the chosen value of \(\alpha\)
Very similar to a regression tree, except that it is used to predict a qualitative response rather than a quantitative one
For a classification tree, we predict that each observation belongs to the most commonly occurring class of training observations in the region to which it belongs
Just as in the regression setting, we use recursive binary splitting to grow a classification tree
In the classification setting, RSS cannot be used as a criterion for making the binary splits
A natural alternative to RSS is the classification error rate. this is simply the fraction of the training observations in that region that do not belong to the most common class:
\(E = 1 - \underset{k}{max} (\hat{P}_{mk})\)
However classification error is not sufficiently sensitive for tree-growing, and in practice two other measures are preferable
The Gini index is defined by
\(G = \sum_{k=1}^{K} \hat{P}_{mk} (1 - \hat{P}_{mk})\)
For this reason the Gini index is referred to as a measure of node purity — a small value indicates that a node contains predominantly observations from a single class
An alternative to the Gini index is cross-entropy, given by
\(D = -\sum_{k=1}^{K} \hat{P}_{mk} \log\hat{P}_{mk}\)
Trees are very easy to explain to people. In fact, they are even easier to explain than linear regression
Some people believe that decision trees more closely mirror human decision-making than do the regression and classification approaches seen in previous chapters
Trees can be displayed graphically, and are easily interpreted even by a non-expert (especially if they are small)
Trees can easily handle qualitative predictors without the need to create dummy variables
Unfortunately, trees generally do not have the same level of predictive accuracy as some of the other regression and classification approaches
However, by aggregating many decision trees, the predictive performance of trees can be substantially improved
Bootstrap aggregation, or bagging, is a general-purpose procedure for reducing the variance of a statistical learning method; we introduce it here because it is particularly useful and frequently used in the context of decision trees
Recall that given a set of \(n\) independent observations \(Z_1, ..., Z_n\) each with variance \(\sigma^2\), the variance of the mean \(\bar{Z}\) of the observations is given by \(\frac{\sigma^2}{n}\)
In other words, averaging a set of observations reduces variance. Of course, this is not practical because we generally do not have access to multiple training sets
Instead, we can bootstrap, by taking repeated samples from the (single) training data set
In this approach we generate \(B\) different bootstrapped training data sets. We then train our method on the \(b_th\) bootstrapped training set in order to get \(\hat{f}^{*b}(x)\), the prediction at a point \(x\). We then average all the predictions to obtain:
\(\hat{f}_{bag}(x) = \frac{1}{B} \sum_{b=1}^{B} \hat{f}^{*b}(x)\)
The above prescription applied to regression trees
For classification trees: for each test observation, we record the class predicted by each of the \(B\) trees, and take a majority vote: the overall prediction is the most commonly occurring class among the \(B\) predictions
It turns out that there is a very straightforward way to estimate the test error of a bagged model
Recall that the key to bagging is that trees are repeatedly fit to bootstrapped subsets of the observations. One can show that on average, each bagged tree makes use of around two-thirds of the observations
The remaining one-third of the observations not used to fit a given bagged tree are referred to as the out-of-bag (OOB) observations
We can predict the response for the ith observation using each of the trees in which that observation was OOB. This will yield around B/3 predictions for the ith observation, which we average
This estimate is essentially the LOO cross-validation error for bagging, if B is large
Random forests provide an improvement over bagged trees by way of a small tweak that decorrelates the trees. This reduces the variance when we average the trees
As in bagging, we build a number of decision trees on bootstrapped training samples
But when building these decision trees, each time a split in a tree is considered, a random selection of m predictors is chosen as split candidates from the full set of \(p\) predictors. The split is allowed to use only one of those \(m\) predictors
A fresh selection of m predictors is taken at each split, and typically we choose \(m \approx \sqrt{p}\) — that is, the number of predictors considered at each split is approximately equal to the square root of the total number of predictors
Like bagging, boosting is a general approach that can be applied to many statistical learning methods for regression or classification. We only discuss boosting for decision trees
Recall that bagging involves creating multiple copies of the original training data set using the bootstrap, fitting a separate decision tree to each copy, and then combining all of the trees in order to create a single predictive model
Notably, each tree is built on a bootstrap data set, independent of the other trees
Boosting works in a similar way, except that the trees are grown sequentially: each tree is grown using information from previously grown trees
Set \(\hat{f}(x) = 0\) and \(r_i = y_i\) for all \(i\) in the training set
For \(b = 1, 2, ..., B\) repeat:
Fit a tree \(\hat{f}^b\) with \(d\) splits (d + 1 terminal nodes) to the training data (X, r)
Update \(\hat{f}\) by adding in a shrunken version of the new tree:
Update the residuals
Output the boosted model
Unlike fitting a single large decision tree to the data, which amounts to fitting the data hard and potentially overfitting, the boosting approach instead learns slowly
Given the current model, we fit a decision tree to the residuals from the model. We then add this new decision tree into the fitted function in order to update the residuals
Each of these trees can be rather small, with just a few terminal nodes, determined by the parameter d in the algorithm
By fitting small trees to the residuals, we slowly improve \(\hat{f}\) in areas where it does not perform well. The shrinkage parameter \(\lambda\) slows the process down even further, allowing more and different shaped trees to attack the residuals
Boosting for classification is similar in spirit to boosting for regression, but is a bit more complex (See chapter 10)
The R package gbm (gradient boosted models) handles a variety of regression and classification problems
The number of trees \(B\). Unlike bagging and random forests, boosting can overfit if \(B\) is too large, although this overfitting tends to occur slowly if at all. We use cross-validation to select \(B\)
The shrinkage parameter \(\lambda\), a small positive number. This controls the rate at which boosting learns. Typical values are 0.01 or 0.001, and the right choice can depend on the problem. Very small \(\lambda\) can require using a very large value of \(B\) in order to achieve good performance
The number of splits d in each tree, which controls the complexity of the boosted ensemble. Often d = 1 works well, in which case each tree is a stump, consisting of a single split and resulting in an additive model. More generally d is the interaction depth, and controls the interaction order of the boosted model, since d splits can involve at most d variables
Decision trees are simple and interpretable models for regression and classification
However they are often not competitive with other methods in terms of prediction accuracy
Bagging, random forests and boosting are good methods for improving the prediction accuracy of trees. They work by growing many trees on the training data and then combining the predictions of the resulting ensemble of trees
The latter two methods— random forests and boosting— are among the state-of-the-art methods for supervised learning. However their results can be difficult to interpret